package fr.dsax.nounet.belote.dao;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import fr.dsax.nounet.belote.jpa.Game;

@Repository
public class GameDaoImpl extends GenericDaoImpl<Game>  implements GameDao {

	public GameDaoImpl() {
		super(Game.class);
	}

	public Game findGameByName(String name) {
		
		CriteriaQuery<Game> findByName = getCriteriaQuery();
		Root<Game> from = findByName.from(Game.class);
		CriteriaQuery<Game> select = findByName.select(from);
		
		Predicate predicate = getCriteriaBuilder().equal(from.get("name"), name);
		
		select.where(predicate);
		TypedQuery<Game> typedQuery = em.createQuery(select);
		List<Game> resultList = typedQuery.getResultList();
		
		return resultList.get(0);
	}
	
	
	
}
